<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户数据表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../layui/css/layui.css" tppabs="static/layui/css/layui.css"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<div class="demoTable">
    &nbsp;&nbsp;姓名：
    <div class="layui-inline">
        <input class="layui-input" name="cname" id="cname" autocomplete="off">
    </div>
    <button class="layui-btn" id="searchBtn" data-type="reload">搜索</button>
</div>

<table class="layui-hide" id="test" lay-filter="test"></table>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="insert">添加</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="det">详情</a>
    <a class="layui-btn layui-btn-xs" lay-event="upd">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script src="../../layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述 JS 路径需要改成你本地的 -->

<script>
    layui.use('table', function(){
        var table = layui.table;
        var $=layui.$;

        table.render({
            elem: '#test'
            ,url:'/ContractServlet?method=queryAll'
            ,method: 'post'
            ,page: true
            ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            ,id:'testReload'
            ,title: '用户数据表'
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field:'cid', title:'ID', width:80,  unresize: true, sort: true}
                ,{field:'harea',title:'房屋名称', width:200,
                    //拼接字符串
                    templet:function (d) {
                        return d.harea + '-' + d.hunitNumber + '-' + d.hroomNo;
                    }
                }
                ,{field:'lname', title:'租户名称', width:150} //edit: 'text'   开启页面修改
                ,{field:'crental', title:'总金额', width:100}
                ,{field:'ccash', title:'押金', width:100}
                ,{field:'cname', title:'签署人', width:200}
                ,{field:'ctime', title:'签署时间', width:200,
                    templet: "<div>{{layui.util.toDateString(d.ctime,'yyyy-MM-dd')}}</div>"
                }
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:200}
            ]]
            , parseData: function (rs) {//数据格式解析
                //console.log(rs);
                if (rs.code === 0) {
                    return {
                        "code": rs.code,
                        "msg": rs.msg,
                        "count": rs.data.totalCount,
                        "data": rs.data.list
                    }
                }
            }
        });


        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'insert':
                    layer.open({
                        type: 2
                        ,title: '添加界面'
                        ,content: "/static/view/contract/add.html"
                        ,area: ["400px", '530px']
                        ,fixed: false
                        ,shadeClose: true,
                        cancel: function () {
                        }, success: function (layero, index) {
                        }, end: function () {
                            table.reload('testReload');
                        }
                    });
                    break;
            };

        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            if(obj.event === 'det'){
                open("详情页面",'/static/view/contract/details.html?cid='+data.cid);
            } else if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    $.ajax({
                        type: "POST",
                        url: "/ContractServlet?method=delete",
                        data: {"cid": data.cid},
                        dataType:"json",
                        success: function (da) {
                            if (da.code === 0) {
                                layer.msg("删除成功！")
                                obj.del(); //删除对应行（tr）的DOM结构
                                // location.reload();//刷新当前页面
                            } else {
                                layer.msg(da.msg)
                            }
                        },
                        error: function (da) {
                            alert("出现错误");
                            return false;
                        }
                    })
                    layer.close(index);
                });
            } else if(obj.event === 'upd'){
                    open("修改页面","/static/view/contract/update.html?cid="+data.cid);
                }
            });

        //打开弹窗
        function open (title,url) {
            layer.open({
                type: 2
                ,title: title
                ,content: url
                ,area: ["400px", "400px"]
                ,fixed: false
                ,shadeClose: true,
                cancel: function () {
                }, success: function (layero, index) {
                }, end: function () {
                    table.reload('testReload');
                }
            });
        }

        //条件查询
        var $ = layui.$, active = {
            reload: function () {
                var cname = $('#cname');
                //执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        "cname": cname.val()
                    }
                });
            }
        };
        $('.demoTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });
</script>

</body>
</html>